/**
 * @filename:OpenAreaServiceImpl 2019-01-17 14:56:43
 * @project skz  V1.3.3
 * Copyright(c) 2018 Liu,Wenzhao Co. Ltd. 
 * All right reserved. 
 */
package com.bingsum.service;

import com.bingsum.annotation.Api;
import com.bingsum.model.OpenArea;
import com.bingsum.util.ApiUtil;
import com.bingsum.util.ParaData;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;

import java.util.List;

import com.bingsum.mapper.OpenAreaMapper;
import tk.mybatis.mapper.entity.Example;

/**   
 *  
 * @Description:  [开放地区表]——SERVICE
 * @Author:       Liu,Wenzhao   
 * @CreateDate:   2019-01-17 14:56:43
 * @Version:      V1.3.3
 *    
 */
@Service("openAreaService")
@Transactional(readOnly = true)
public class OpenAreaService{

    @Autowired
    private OpenAreaMapper openAreaMapper;

    @Api
    public Object getAreaInfoList(ParaData pd) {
        Page<?> page = PageHelper.startPage(pd.getInteger("currentPage"), 20);
        Example example = new Example(OpenArea.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNull("deleteTime");
        openAreaMapper.selectByExample(example);
        return ApiUtil.returnObject(pd, page);
    }

    @Api
    public Object getAreaInfo(ParaData pd) {
        Example example = new Example(OpenArea.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNull("deleteTime");
        criteria.andEqualTo("id", pd.getString("id"));
        OpenArea openArea = openAreaMapper.selectOneByExample(example);

        if (openArea == null){
            return ApiUtil.returnDescFail(pd,"无返回");
        }
        return openArea;
    }

    @Api
    @Transactional(readOnly = false)
    public Object newAreaInfo(ParaData pd) {
        OpenArea openArea= pd.toBean(OpenArea.class);
        if (openArea.getId() != null) {
            this.openAreaMapper.updateByPrimaryKeySelective(openArea);
        } else {
            this.openAreaMapper.insert(openArea);
        }
        return ApiUtil.returnOK(pd,openArea);
    }

    @Api
    @Transactional(readOnly = false)
    public Object delArea(ParaData pd) {
        openAreaMapper.delArea(pd);
        OpenArea openArea= pd.toBean(OpenArea.class);
        return ApiUtil.returnOK(pd,openArea.getId());
    }
}